#!/usr/bin/env python
import sys
sys.path.insert(0,'../src')

import unittest
import x4gllexer

def token2tuple(tok):
    '''To simply test comparisions return token properties as a tuple.'''
    return(tok.type, tok.value)

class DefineTestCase(unittest.TestCase):
    def setUp(self):
        self.lexer = x4gllexer.lexer
        
    def tearDown(self):
        self.lexer = None
        
    def testDefineCharWithNoDimension(self):
        '''Define a CHAR datatype with no dimension'''
        self.lexer.input('define a char')
        self.assertEqual(('DEFINE', 'define'), token2tuple(self.lexer.token()))
        self.assertEqual(('IDENTIFIER', 'a'), token2tuple(self.lexer.token()))
        self.assertEqual(('CHAR', 'char'), token2tuple(self.lexer.token()))
        
    def testDefineCharWithDimension(self):
        '''Define a CHAR datatype with dimension'''
        self.lexer.input('define a char(10)')
        self.assertEqual(('DEFINE', 'define'), token2tuple(self.lexer.token()))
        self.assertEqual(('IDENTIFIER', 'a'), token2tuple(self.lexer.token()))
        self.assertEqual(('CHAR', 'char(10)'), token2tuple(self.lexer.token()))
        
    def testDefineCharWithBadDimension(self):
        '''Define a CHAR datatype with bad dimension'''
        self.lexer.input('define a char(10.0)')
        self.assertEqual(('DEFINE', 'define'), token2tuple(self.lexer.token()))
        self.assertEqual(('IDENTIFIER', 'a'), token2tuple(self.lexer.token()))
        self.assertNotEqual(('CHAR', 'char(10.0)'), token2tuple(self.lexer.token()))
        
        
class EndTestCase(unittest.TestCase):
    def setUp(self):
        self.lexer = x4gllexer.lexer
        
    def tearDown(self):
        self.lexer = None
        
    def testEndIf(self):
        '''Identify END IF'''
        self.lexer.input('end if')
        self.assertEqual(('END_IF', 'if'), token2tuple(self.lexer.token()))
        
    def testEndWhile(self):
        '''Identify END WHILE'''
        self.lexer.input('end while')
        self.assertEqual(('END_WHILE', 'while'), token2tuple(self.lexer.token()))
        
    def testEndFor(self):
        '''Identify END FOR'''
        self.lexer.input('end for')
        self.assertEqual(('END_FOR', 'for'), token2tuple(self.lexer.token()))
        
    def testEndForeach(self):
        '''Identify END FOREACH'''
        self.lexer.input('end foreach')
        self.assertEqual(('END_FOREACH', 'foreach'), token2tuple(self.lexer.token()))

if __name__ == '__main__':
    unittest.main()
